package com.tanhua.api.impl.mongo;


import com.tanhua.api.houtai.SoulApi;
import com.tanhua.domain.mongo.Soul;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author user
 */
@Service
public class SoulApiImpl implements SoulApi {
    @Autowired
    public MongoTemplate mongoTemplate;

    /**
     * 查询试卷列表
     *
     * @return
     */
    @Override
    public List<Soul> getSoulList() {
        Query query = new Query();
        List<Soul> soulList = mongoTemplate.find(query, Soul.class);
        return soulList;
    }

    /**
     * 根据id查询试卷
     *
     * @param soulId
     * @return
     */
    @Override
    public Soul findSoulId(Integer soulId) {
        Query query = new Query(Criteria.where("soulId").is(soulId));
        Soul soul = mongoTemplate.findOne(query, Soul.class);
        return soul;
    }

    /**
     * 然后根据id删除试卷
     *
     * @param soulId
     */
    @Override
    public void deleteSoulId(Integer soulId) {
        Query query = new Query(Criteria.where("soulId").is(soulId));
        mongoTemplate.remove(query, "soul_test_list");
    }

    /**
     * 添加试卷
     *
     * @param soul
     */
    @Override
    public void addSoul(Soul soul) {
        mongoTemplate.insert(soul, "soul_test_list");
    }

    /**
     * 修改试卷
     *
     * @param soul
     */
    @Override
    public void updateSoul(Soul soul) {
        Query query = new Query(Criteria.where("soulId").is(soul.getSoulId()));
        Update update = new Update();
        if (!soul.getName().equals(null)) {
            update.set("name", soul.getName());
        }
        update.set("questionIds", soul.getQuestionIds());
        mongoTemplate.upsert(query, update, Soul.class);

    }

}
